package com.dpiinc.TechPage.user.logic;

import android.util.Log;

import com.dpiinc.TechPage.AIWriteApplication;
import com.dpiinc.TechPage.UserManager;
import com.dpiinc.TechPage.api.BaseObserver;
import com.dpiinc.TechPage.api.RetrofitFactory;
import com.dpiinc.TechPage.api.RxSchedulers;
import com.dpiinc.TechPage.model.BaseEntity;
import com.dpiinc.TechPage.model.jbean.RecognizeApiKeyBean;

import java.util.HashMap;

import io.reactivex.Observable;

/**
 * Created by hu489_000 on 2018-08-07.
 */

public class RecognizeApiGetHelper {

    private static final String TAG = "RecognizeApiGetHelper";
    /**
     * 从服务器获取apikey
     *
     */
    public static void recognizeApiGet() {
//        ToastUtils.showTopTip(context,getResources().getString(R.string.recognizing));
        HashMap<String, String> requestMap = getRecognizeGetMap();
        Observable<BaseEntity<RecognizeApiKeyBean>> observable = RetrofitFactory.getInstance().recognizeApiGet(requestMap);
        BaseObserver mBaseObserver = new BaseObserver<RecognizeApiKeyBean>(AIWriteApplication.Companion.instance()) {
            @Override
            protected void onHandleError(int code) {
                super.onHandleError(code);
                Log.d(TAG, "recognizeGet onHandleError code = " + code);
            }

            @Override
            protected void onHandleSuccess(RecognizeApiKeyBean baseBean) {
                Log.d(TAG, "recognizeGet onHandleSuccess RecognizeApiKeyBean = " + baseBean);

                if (baseBean != null) {
                    SpUtils.putRecognizeApiKeyBean(AIWriteApplication.Companion.instance(), baseBean);
                }

            }
        };
        observable.compose(RxSchedulers.compose()).subscribe(mBaseObserver);
    }

    private static HashMap<String, String> getRecognizeGetMap() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uId", UserManager.INSTANCE.getUserId());
        params.put("accessToken", UserManager.INSTANCE.getAccessToken());
        return params;
    }
}
